angular.module('myApp')
.service('Dialog',function($q){
	var $overlay = $('<div class="overlay"></div>').hide().appendTo(document.body);
	var $dialog = $('<div class="dialog-body">\
						<div class="dialog-title"></div>\
						<div class="dialog-content"></div>\
						<div class="dialog-footer">\
							<button class="confirm">确定</button>\
							<button class="cancel">取消</buttom>\
						</div>\
					</div>').hide().appendTo(document.body);
	var $btnConfirm = $dialog.find('.confirm');
	var $btnCancel = $dialog.find('.cancel');
	var $dialogTitle = $dialog.find('.dialog-title');
	var $dailogContent = $dialog.find('.dialog-content');


	var deferred;
	var self = this;
	$btnConfirm.on('click',function(){
		deferred.resolve('ok');
		self.hide();
	});

	$btnCancel.on('click',function(){
		deferred.reject('ok');
		self.hide();
	});

	var defaultOptions = {
		title: '提示信息',
		content: '确认吗',
		confirmBtnText: '确定',
		cancelBtnText: '取消'
	}

	this.show = function(opt){
		var options = $.extend({},defaultOptions,opt);
		$dialogTitle.html(options.title);
		$dailogContent.html(options.content);
		$btnConfirm.text(options.confirmBtnText);
		$btnCancel.text(options.cancelBtnText);

		deferred = $q.defer();
		$overlay.fadeIn(500);
		$dialog.fadeIn(500);

		return deferred.promise;
	}

	this.hide = function(){
		$overlay.fadeOut(500);
		$dialog.fadeOut(500);		
	}
});
